$(function () {
    // 点击添加按钮后跳转到的页面
    $("#add_device").click(function (event) {
        event.preventDefault();
        $.ajax({
            type: "GET",
            success: function (msg) {
                window.location.href = "add_device";
            }
        })
    });

    // 点击返回按钮跳转的页面
    $("#back").click(function (event) {
        event.preventDefault();
        $.ajax({
            type: "GET",
            success: function (msg) {
                window.location.href = "/admin";
            }
        })
    });

    // 添加新设备
    $("#add_confirm").click(function (event) {
        event.preventDefault();
        var serialnumber_element = $("#serialnumber");
        var model_element = $("#model");
        var cpu_model_element = $("#cpu_model");
        var cpu_cores_element = $("#cpu_cores");
        var bmc_address_element = $("#bmc_address");
        var purpose_element = $("#purpose");
        var owner_element = $("#owner");
        var others_element = $("#others");

        var serialnumber = serialnumber_element.val();
        var model = model_element.val();
        var cpu_model = cpu_model_element.val();
        var cpu_cores = cpu_cores_element.val();
        var bmc_address = bmc_address_element.val();
        var purpose = purpose_element.val();
        var owner = owner_element.val();
        var others = others_element.val();

        $.ajax({
            type: "POST",
            url: "/admin/add_device",
            data: {
                'serialnumber': serialnumber,
                'model': model,
                'cpu_model': cpu_model,
                'cpu_cores': cpu_cores,
                'bmc_address': bmc_address,
                'purpose': purpose,
                'owner': owner,
                'others': others
            },
            dataType: "json",
            success: function (data) {
                if (data.code == 200) {
                    window.location.href = "/admin";
                    alert(data.message);
                } else {
                    console.log(data);
                    alert(data.message);
                }
            }
        })
    });

    // 点击确定删除按钮
    $(".confirm_delete").click(function (event) {
        event.preventDefault();
        var serialnumber = $("#confirm_del_dev").val();

        $.ajax({
            type: "POST",
            url: "/admin/del_device",
            data: {
                'serialnumber': serialnumber
            },
            datatype: "json",
            success: function (data) {
                if (data.code == 200) {
                    window.location.href = "/admin";
                } else {
                    console.log(data);
                    alert(data.message);
                }
            }
        })

    });


    // 点击编辑按钮后跳转到的页面
    $(".edit_device").click(function (event) {
        event.preventDefault();
        // 获取当前编辑按钮的祖父元素<tr></tr>
        var tr_element = $(this).parent().parent();

        var serialnumber = tr_element.attr('sid');
        var model = tr_element.attr('model');
        var cpu_model = tr_element.attr('cpu_model');
        var cpu_cores = tr_element.attr('cpu_cores');
        var bmc_address = tr_element.attr('bmc_address');
        var purpose = tr_element.attr('purpose');
        var owner = tr_element.attr('owner');
        var others = tr_element.attr('others');

        $.ajax({
            type: "GET",
            success: function (msg) {
                // window.location.href = "edit_device?serialnumber=" + serialnumber + "&model=" + model + "&cpu_model=" + cpu_model + "&cpu_cores=" + cpu_cores;
                window.location.href = "edit_device?serialnumber=" + serialnumber;
            }
        })
    });

    // 点击修改按钮
    $("#edit_confirm").click(function (event) {
        event.preventDefault();
        var serialnumber_element = $("#serialnumber");
        var model_element = $("#model");
        var cpu_model_element = $("#cpu_model");
        var cpu_cores_element = $("#cpu_cores");
        var bmc_address_element = $("#bmc_address");
        var purpose_element = $("#purpose");
        var owner_element = $("#owner");
        var others_element = $("#others");

        var serialnumber = serialnumber_element.val();
        var model = model_element.val();
        var cpu_model = cpu_model_element.val();
        var cpu_cores = cpu_cores_element.val();
        var bmc_address = bmc_address_element.val();
        var purpose = purpose_element.val();
        var owner = owner_element.val();
        var others = others_element.val();

        $.ajax({
            type: "POST",
            url: "/admin/edit_device",
            data: {
                'serialnumber': serialnumber,
                'model': model,
                'cpu_model': cpu_model,
                'cpu_cores': cpu_cores,
                'bmc_address': bmc_address,
                'purpose': purpose,
                'owner': owner,
                'others': others
            },
            datatype: "json",
            success: function (data) {
                if (data.code == 200) {
                    window.location.href = "/admin";
                    alert(data.message);
                } else {
                    console.log(data);
                    alert(data.message);
                }
            }
        })
    });

    // 点击登录按钮
    $("#login").click(function (event) {
        event.preventDefault();
        var username_element = $("#username");
        var password_element = $("#password");

        var username = username_element.val();
        var password = password_element.val();

        $.ajax({
            type: "POST",
            url: "/admin/login",
            data: {
                'username': username,
                'password': password
            },
            datatype: "json",
            success: function (data) {
                if (data.code == 200) {
                    window.location.href = "/admin";
                } else {
                    console.log(data);
                    alert(data.message);
                }
            }
        })

    });

    // 点击登出按钮
    $("#logout").click(function (event) {
        event.preventDefault();
        $.ajax({
            type: "GET",
            url: '/admin/logout',
            success: function (data) {
                if (data.code == 200) {
                    window.location.href = '/admin/login';
                }
            }
        })
    });

});
